﻿using SimpleTerminal.Common;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Linq;

namespace SimpleTerminal
{
    public partial class SelectProfileForm : Form
    {
        List<ProfileInfo> profileInfos;
        public event EventHandler<ProfileInfo> SelectedProfile;


        public SelectProfileForm()
        {
            InitializeComponent();


            AddColumnHeader("名字", 122);
            AddColumnHeader("主机名", 180);
            AddColumnHeader("端口", 40);
            AddColumnHeader("用户名", 90);
            LoadData();
            // UpdateView();
        }

        private void AddColumnHeader(string name, int width)
        {
            ColumnHeader ch = new ColumnHeader
            {
                Text = name,   //设置列标题
                Width = width,    //设置列宽度
                TextAlign = HorizontalAlignment.Left   //设置列的对齐方式
            };
            this.listView.Columns.Add(ch);
        }



        private void LoadData()
        {
            // profileInfos.Clear();
            using (ProfileModel profileModel = new ProfileModel())
            {
                profileInfos = profileModel.GetAll();
            }

            this.listView.Items.Clear();
            this.listView.BeginUpdate();
            foreach (ProfileInfo profile in profileInfos)   //添加10行数据
            {
                ListViewItem lvi = new ListViewItem
                {
                    Text = profile.Name,
                    Tag = profile
                };
                lvi.SubItems.Add(profile.Host);
                lvi.SubItems.Add("" + profile.Port);
                lvi.SubItems.Add("" + profile.Account);
                this.listView.Items.Add(lvi);
            }
            this.listView.EndUpdate();  //结束数据处理，UI界面一次性绘制。

          
        }
       

       


        private void ListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {

        }

        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {

            //  Console.WriteLine(listView.SelectedItems.Count);
        }

        private void BtnCreate_Click(object sender, EventArgs e)
        {
            ConnectInfo connectInfo = new ConnectInfo();
            connectInfo.StartPosition = FormStartPosition.CenterParent;
            if (connectInfo.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }

        private void ListView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListViewHitTestInfo listViewHit = listView.HitTest(e.X, e.Y);
            if (listViewHit.Item == null)
                return;
            ProfileInfo profileInfo = listViewHit.Item.Tag as ProfileInfo;
            //if (connectInfo.ShowDialog() == DialogResult.OK)
            //{
            //    LoadData();
            //}
            ConnectAndCloseForm(profileInfo);

        }


        private void ConnectAndCloseForm(ProfileInfo profileInfo)
        {
            if (profileInfo == null)
                return;
            if (SelectedProfile != null)
                SelectedProfile(this, profileInfo);
            this.Close();
        }

        private void ListView_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ListViewItem item = listView.GetItemAt(e.X, e.Y);
                if (item != null)
                {
                    this.contextMenuStrip1.Show(listView, e.X, e.Y);
                }
            }
        }

        private void DeleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView.SelectedItems.Count > 0)
            {
                ListViewItem item = listView.SelectedItems[0];
                ProfileInfo profileInfo = item.Tag as ProfileInfo;
                using (ProfileModel profileModel = new ProfileModel())
                {
                    profileModel.DeleteProfile(profileInfo);
                }
                LoadData();
            }
        }

        private void ConnectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView.SelectedItems.Count > 0)
            {
                ListViewItem item = listView.SelectedItems[0];
                // ConnectAndCloseForm((ProfileInfo)profileInfos[item.Index]);
            }
        }

        private void EditToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView.SelectedItems.Count > 0)
            {
                ProfileInfo profileInfo = listView.SelectedItems[0].Tag as ProfileInfo;
                
                ConnectInfo connectInfo = new ConnectInfo((int)profileInfo.Id);
                connectInfo.StartPosition = FormStartPosition.CenterParent;
                // connectInfo.SetProfile(index, (ProfileInfo)profileInfos[index]);
                //  connectInfo.Save += ConnectInfo_Save;
                if (connectInfo.ShowDialog() == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }



        private void BtnReload_Click(object sender, EventArgs e)
        {
            LoadData();

        }

        private void SelectProfileForm_Load(object sender, EventArgs e)
        {
            this.checkBox1.Checked = Preference.Default.Startup_Open;
        }

        private void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            Preference.Default.Startup_Open = this.checkBox1.Checked;
        }
    }
}
